from django.db import models
from django.contrib.auth.models import AbstractUser
# from django.contrib.auth.backends import Permission  导入则报错

<<<<<<< HEAD
=======
# 其他测试
class PhoneCollection(models.Model):
    mobile = models.CharField("手机号", max_length=11)
    mark = models.CharField("备注", max_length=200)

    def __str__(self):
        return "收集到%s"%self.mobile

    class Meta:
        db_table = "phone_tb"
        verbose_name_plural = "手机号表"
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9

# Create your models here.
class User(AbstractUser):

    mobile = models.CharField("手机号", max_length=11, default="", unique=False)
    last_login = models.DateTimeField(null=True)

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'user_tb'
        verbose_name_plural = "用户"


# 权限类型表 （角色）
# id   name
# pk  varchar(30)
class PermissionType(models.Model):
    name = models.CharField("权限类型", max_length=30)
<<<<<<< HEAD

    def __str__(self):
        return self.name

=======

    def __str__(self):
        return self.name

>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9
    class Meta:
        db_table = "permission_type"
        verbose_name_plural = "权限类型"


# 权限
# id    title        code_name    type_name
# pk  varchar(30)    varchar(30)   fk
class Permission(models.Model):
<<<<<<< HEAD
    name = models.CharField("权限名称", max_length=30)
=======
    title = models.CharField("权限名称", max_length=30)
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9
    code_name = models.CharField("权限代码", max_length=30)
    type_name = models.ForeignKey(PermissionType, on_delete=models.CASCADE, verbose_name="权限类型")

    def __str__(self):
<<<<<<< HEAD
        return self.name

    class Meta:
        db_table = "permission"
        verbose_name_plural = "用户权限"
=======
        return self.title

    class Meta:
        db_table = "permission"
        verbose_name_plural = "权限"
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9


# 用户组
# id    name       user     permission
<<<<<<< HEAD
# pk  varchar(30)  m:n      m:n
class UserGroup1(models.Model):
    name = models.CharField("用户组名", max_length=30)
    user = models.ManyToManyField(User)
    permission = models.ManyToManyField(Permission)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "user_group"
        verbose_name_plural = "用户组"


=======
# pk  varchar(30)  m:m      m:m
class Group(models.Model):
    name = models.CharField("用户组名", max_length=30)
    user = models.ManyToManyField(User)
    permission = models.ManyToManyField(Permission)
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9

    def  __str__(self):
        return self.name

    class Meta:
        db_table = "group"
        verbose_name_plural = "用户组"










# 演示多对多
class Author(models.Model):
    name = models.CharField("作者姓名", max_length=30)
    age = models.IntegerField("年龄", default=20)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "author_tb"


class Book(models.Model):
    name = models.CharField("书籍名字", max_length=30, unique=True)
    author = models.ManyToManyField(Author)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "book_tb"


class Cate(models.Model):
    """
    商品分类
    """
    name = models.CharField(max_length=30)
    # 自关联： 自己关联自己
    cid = models.ForeignKey('self', related_name='subs',on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        db_table = 'cate_tb'

    def __str__(self):
        return self.name


class Payment(models.Model):
    """支付信息"""
    trade_id = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="支付编号")
    order = models.ForeignKey(Cate, on_delete=models.CASCADE, verbose_name='订单')



